package com.example.my.meila.view.activity;

import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.example.my.meila.R;
import com.example.my.meila.common.utils.AppManager;
import com.example.my.meila.common.utils.FragmentTabUtils;
import com.example.my.meila.view.fragment.BeautyFragment;
import com.example.my.meila.view.fragment.GlobalFragment;
import com.example.my.meila.view.fragment.MeFragment;
import com.example.my.meila.view.fragment.RankFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.jpush.android.api.JPushInterface;


public class MainActivity extends AppCompatActivity {
    private FragmentTransaction transaction;
    private FragmentManager manager;
    @BindView(R.id.ll_main)
    LinearLayout llMain;
    @BindView(R.id.rg_bar)
    RadioGroup rg;
    private List<Fragment> fragments = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        manager = getSupportFragmentManager();
        transaction = manager.beginTransaction();


        ButterKnife.bind(this);
        fragments.add(BeautyFragment.newInstance());
        fragments.add(RankFragment.newInstance());
        fragments.add(GlobalFragment.newInstance());
        fragments.add(MeFragment.newInstance());

        FragmentTabUtils utils = new FragmentTabUtils(
                getSupportFragmentManager()
                , fragments
                , R.id.ll_main
                , rg
        );
    }

    private long firstTime;

    @Override
    public void onBackPressed() {
        long secondTime = System.currentTimeMillis();
        if (secondTime - firstTime > 2000) {                                         //如果两次按键时间间隔大于2秒，则不退出
            Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
            firstTime = secondTime;
        } else {
            //两次按键小于2秒时，退出应用
            //注销网络监听
            App.getInstance().unRegisterNetworkStateReceiver();
            //注销声明周期管理
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
                App.getInstance().unRegisterActivityLifecycleCallbacks();
            }
            AppManager.getAppManager().finishActivity(this);
            AppManager.getAppManager().AppExit(App.getInstance());
        }
    }


    @Override
    protected void onPause() {
        super.onPause();
        JPushInterface.onPause(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        JPushInterface.onResume(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        JPushInterface.clearAllNotifications(this);
    }
}
